package com.simon.hse.activity.law;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.LawDetailOutput;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 法律法规管理-详情
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_law_detail)
public class LawDetailActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.tv_law_detail_title)
    TextView mTv_law_detail_title;

    @ViewInject(R.id.tv_law_detail_subjet)
    TextView mTv_law_detail_subjet;

    @ViewInject(R.id.tv_law_detail_subjet_time)
    TextView mTv_law_detail_subjet_time;

    @ViewInject(R.id.tv_law_detail_clause)
    TextView mTv_law_detail_clause;

    @ViewInject(R.id.tv_law_detail_describe)
    TextView mTv_law_detail_describe;

    @ViewInject(R.id.tv_law_detail_symbol)
    TextView mTv_law_detail_symbol;

    @ViewInject(R.id.tv_law_detail_issue_date)
    TextView mTv_law_detail_issue_date;

    @ViewInject(R.id.tv_law_detail_implementation_date)
    TextView mTv_law_detail_implementation_date;

    @ViewInject(R.id.tv_law_detail_gkbm)
    TextView mTv_law_detail_gkbm;

    @ViewInject(R.id.tv_law_detail_zxbm)
    TextView mTv_law_detail_zxbm;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;
    @ViewInject(R.id.tv_law_detail_change_date)
    TextView mTv_law_detail_change_date;

    private String id;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<LawDetailOutput.DataBean.StandardInformationDeptBean> mDataBeanList = new ArrayList<>();// 详情

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("法律法规管理");

        Bundle bundle = getIntent().getExtras();
        if (bundle == null)
            return;
        id = bundle.getString("ID");

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
        }
    }

    private void initRecyclerView() {
        LinearLayoutManager llm = new LinearLayoutManager(LawDetailActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(LawDetailActivity.this, R.layout.layout_item_law_detail, mDataBeanList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<LawDetailOutput.DataBean.StandardInformationDeptBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, LawDetailOutput.DataBean.StandardInformationDeptBean dataBean) {
                TextView tv_law_detail_stand_1 = holder.getView(R.id.tv_law_detail_stand_1);
                TextView tv_law_detail_stand_2 = holder.getView(R.id.tv_law_detail_stand_2);
                TextView tv_law_detail_stand_3 = holder.getView(R.id.tv_law_detail_stand_3);
                TextView tv_law_detail_stand_4 = holder.getView(R.id.tv_law_detail_stand_4);

                tv_law_detail_stand_1.setText(dataBean.getDepartmentName());
                tv_law_detail_stand_2.setText(dataBean.getDeptType());
                tv_law_detail_stand_3.setText(dataBean.getFitSituation());
                tv_law_detail_stand_4.setText(dataBean.getFitSituationOrNotDescribe());
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {

            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        Call<String> call = HttpManager.getInstance().requestService().LAW_DETAIL(id);
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                LawDetailOutput lawDetailOutput = new Gson().fromJson(result, LawDetailOutput.class);
                if (lawDetailOutput.getResult().equals("Success")) {
                    LawDetailOutput.DataBean dataBean = lawDetailOutput.getData();
                    mTv_law_detail_title.setText(dataBean.getLawRegulationsName());
                    mTv_law_detail_subjet.setText(dataBean.getIssueSubject());
                    mTv_law_detail_subjet_time.setText("最新修改时间：" + dataBean.getLastUpdateDate());
                    mTv_law_detail_change_date.setText(dataBean.getLastUpdateDate());
                    mTv_law_detail_clause.setText(dataBean.getApplyClause());
                    mTv_law_detail_describe.setText(dataBean.getDescribeForWork());
                    mTv_law_detail_symbol.setText(dataBean.getSymbol());
                    mTv_law_detail_issue_date.setText(dataBean.getIssueDate());
                    mTv_law_detail_implementation_date.setText(dataBean.getImplementationDate());
                    List<String> gkbmList = dataBean.getGkbmList();
                    StringBuffer sb1 = new StringBuffer();
                    for (String s : gkbmList) {
                        sb1.append(s + " ");
                    }
                    mTv_law_detail_gkbm.setText(sb1.toString());
                    List<String> zxbmList = dataBean.getZxbmList();
                    StringBuffer sb2 = new StringBuffer();
                    for (String s : zxbmList) {
                        sb2.append(s + " ");
                    }
                    mTv_law_detail_zxbm.setText(sb2.toString());

                    List<LawDetailOutput.DataBean.StandardInformationDeptBean> standardInformationDeptBeanList = dataBean.getStandardInformationDept();
                    if (!standardInformationDeptBeanList.isEmpty()) {
                        mDataBeanList.addAll(standardInformationDeptBeanList);
                        mCommonEmptyAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtils.getInstance().showShortToastBottom(lawDetailOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
